﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using InnvoiceActual.Logica.GestorDB;
using InnvoiceActual.Logica.GestorObject.Articulo;

namespace InnvoiceActual.Ws.Articulos.Categorias
{
    /// <summary>
    /// Interaction logic for AltaCategoria.xaml
    /// </summary>
    public partial class AltaCategoria : Window
    {
        #region Property

        private CategoriaArticulo nuevaCategoria = new CategoriaArticulo();

        #endregion

        #region Constructor

        public AltaCategoria()
        {
            InitializeComponent();
            DataContext = nuevaCategoria;
            nuevaCategoria.PropertyChanged += new PropertyChangedEventHandler(nuevaCategoria_PropertyChanged);
            this.TextNombre.Focus();
        }

        #endregion

        #region Windows Methods

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();

        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;

            }
        }

        private void nuevaCategoria_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = nuevaCategoria.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            ArticuloDB articuloDb = new ArticuloDB();
            articuloDb.AddCategoriaArticulo(nuevaCategoria);
            this.Close();
        }

        #endregion
    }
}
